int led_pin = 13;

//0: Eating, 1: Ate, 2: Idle
int state;

int ID = 0;
int irPin1 = 2;
int irPin2 = 3;

void setup() {
  state = 2;
  pinMode(led_pin, OUTPUT);
  pinMode(irPin1, OUTPUT);
  pinMode(irPin2, OUTPUT);
  
  digitalWrite(irPin1, HIGH);
  digitalWrite(irPin2, HIGH);
  
  Serial.begin(9600);
}

void loop() {
  // Read incoming information from Processing
  if( Serial.available() > 1 ) {
    char result = Serial.read();
    
    //while( Serial.available() < 0 ) { }
    char fId = Serial.read();
    
    if( fId != ID ) return;
    
    if( result == 'E' ) state = 0;
    else if( result == 'A' ) state = 1;
    else if( result == 'I' ) state = 2;
  }
    
  switch(state) {
    case 0: // Eating
    {
      digitalWrite(led_pin, HIGH);
      break;
    }
    case 1: // Ate
    {
      digitalWrite(led_pin, HIGH);
      delay(100);
      digitalWrite(led_pin, LOW);
      delay(100);
      break;
    }
    case 2: // Idle
    {
      digitalWrite(led_pin, LOW);   
      break;
    }
  }
}
